var api = require("../../util/util.js")
var app = getApp()
var service_common = require("../../service/common.js")
var service_version = require("../../service/version.js")
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		code: '',
		open_id: '',
		session_key: '',
		returnData: {},
		options: null
	},
	onLoad: function(options) {
    // scene=1403255%2C444
		this.setData({
			options: options
		})
    getApp().globalData.uniq_openid = options.uniq_openid
		this.check()
	},

	// 检测授权
	check: function() {
		let that = this
		// 判断是否已经授权
		wx.getSetting({
			success: (rel) => {
				if (rel.authSetting['scope.userInfo']) {
					// 已开启授权   
					let userInfo = wx.getStorageSync('userInfo');
					app.globalData.userInfo = userInfo
					app.globalData.openid = userInfo.openid
					app.globalData.unionid = userInfo.unionid
          if (JSON.stringify(userInfo) == '{}' || JSON.stringify(userInfo)=='') {
						that.getUserInfoFun()
					} else {
            if (this.data.options.weburl != undefined) {
            
              if (this.data.options.weburl == 'other') {
                wx.navigateTo({
                  url: '/pages/' + this.data.options.weburl + '/' + this.data.options.weburl + '?data=0&openid=' + this.data.options
                    .openid,
                })
              } else if (this.data.options.weburl == 'aRed') {
                wx.navigateTo({
                  url: '/pages/' + this.data.options.weburl + '/' + this.data.options.weburl + '?data=0&id=' + this.data.options
                    .id,
                })
              } else if (this.data.options.weburl == 'aHome') {
                wx.navigateTo({
                  url: '/pages/' + this.data.options.weburl + '/' + this.data.options.weburl + '?data=0&id=' + this.data.options
                    .roomID,
                })
              } else if (this.data.options.weburl == 'wrq'){
                wx.navigateTo({
                  url: '/pages/new_demand_detail/new_demand_detail' + '?data=0&id=' + this.data.options.id,
                })
              } else if (this.data.options.weburl == 'shop_index'){
                wx.switchTab({
                  url: '/pages/shop_index/shop_index?data=0&share_user_id=' + this.data.options.share_user_id,
                })
              } else if (this.data.options.weburl == 'goods_detail') {
                wx.navigateTo({
                  url: '/shop/goods_detail/goods_detail?data=0&share_user_id=' + this.data.options.share_user_id + "&goods_id=" + this.data.options.goods_id,
                })
              }
            }else{
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
					}
				} else {
					// 未开启授权
					wx.login({
						success: (res) => {
							that.setData({
								code: res.code
							})
							that.getUserInfoFun()
						}
					})
				}
			}
		})
	},

	// 点击授权
	getUserInfoFun: function() {
		let that = this
		wx.getUserInfo({
			success: (res) => {
				// 获取授权信息
				that.setData({
					returnData: res
				})
				that.getOpenid()
			},
			fail: function(err) {
				console.log('获取授权信息失败')
				console.log(err)
			}
		})
	},

	// 获取openid
	getOpenid: function() {
		let that = this
		wx.login({
			success: (res) => {
				service_common.fetchData(
					service_common.getUrl("user.getOpenid"), {
						code: res.code
					},function(result) {
						if (result.data.code == 0) {
							let open_id = result.data.data.openid
              wx.setStorageSync("real_openid", result.data.data.openid)
							let session_key = result.data.data.session_key
							that.setData({
								open_id: open_id,
								session_key: session_key,
							})
							that.appLogin(that.data.options.openid)
						} else {
							api.tips(result.data.message)
						}
					},
					"GET"
				)
			},
			fail: (err) => {
				console.log('获取openid失败')
			}
		})
	},

	// 调用登录接口
	appLogin: function(shareopenid = 0) {
		let that = this
		service_common.fetchData(
			service_common.getUrl("user.loginByWechat"), {
				openid: that.data.open_id,
				sessionkey: that.data.session_key,
				encryptedData: that.data.returnData.encryptedData,
				iv: that.data.returnData.iv,
				shareopenid: shareopenid,
        uniq_openid: that.data.options.uniq_openid
			},
			(res) => {
				if (res.data.code == 0) {
					wx.setStorageSync('userInfo', res.data.data);
          wx.setStorageSync('token__', res.data.data.token)
          wx.setStorageSync('token_key', res.data.data.token_key)
					app.globalData.userInfo = res.data.data
					app.globalData.openid = res.data.data.openid
					app.globalData.unionid = res.data.data.unionid

          if (this.data.options.weburl != undefined) {
            if (this.data.options.weburl == 'other') {
              wx.navigateTo({
                url: '/pages/' + this.data.options.weburl + '/' + this.data.options.weburl + '?data=0&openid=' + this.data.options
                  .openid,
              })
            } else if (this.data.options.weburl == 'aRed') {
              wx.navigateTo({
                url: '/pages/' + this.data.options.weburl + '/' + this.data.options.weburl + '?data=0&id=' + this.data.options
                  .id,
              })
            } else if (this.data.options.weburl == 'aHome') {
              wx.navigateTo({
                url: '/pages/' + this.data.options.weburl + '/' + this.data.options.weburl + '?data=0&id=' + this.data.options
                  .roomID,
              })
            } else if (this.data.options.weburl == 'wrq') {
              wx.navigateTo({
                url: '/pages/new_demand_detail/new_demand_detail' + '?data=0&id=' + this.data.options.id,
              })
            } else if (this.data.options.weburl == 'shop_index') {
              wx.switchTab({
                url: '/pages/shop_index/shop_index?data=0&share_user_id=' + this.data.options.share_user_id,
              })
            }else if (this.data.options.weburl == 'goods_detail') {
              wx.navigateTo({
                url: '/shop/goods_detail/goods_detail?data=0&share_user_id=' + this.data.options.share_user_id + "&goods_id=" + this.data.options.goods_id,
              })
            }
          } else {
            wx.reLaunch({
              url: '/pages/index/index',
            })
          }
				} else {
					api.tips(res.data.message)
				}
			},
			"GET"
		)
	}
})
